﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace Common
{
    public class Cls_ProjectTable:BaseNode
    {
        public Cls_ProjectTable(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }

        private List<Cls_PTRow> _list = null;

        public List<Cls_PTRow> Cls_PTRows
        {
            get
            {
                Cls_PTRow c = null;

                if (null != _list)
                    _list.Clear();
                else
                    _list = new List<Cls_PTRow>();

                foreach (XmlNode x in _XmlNode.SelectNodes("PTRow"))
                {
                    c = new Cls_PTRow(x);              
                    _list.Add(c);
                }
                _list.Sort();
                return _list;
            }
        }

        public Cls_PTRow PTRow(int id)
        {
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;

            //add new task
            ch = _XmlNode.SelectSingleNode("PTRow[@id=" + id.ToString() + "]");
            if (ch == null) return null;
            return new Cls_PTRow(ch);
        }

        public void Sort()
        {
 
        }

        //Add
        public Cls_PTRow AddPTRow(int id) 
        {
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;
            Cls_PTRow ExistCh = PTRow(id);

            if (null != ExistCh) return ExistCh;

            //add new 
            ch = _XmlNode.AppendChild(doc.CreateElement("PTRow"));
            ch.Attributes.Append(doc.CreateAttribute("id")).Value = id.ToString();

            //set property
            ch.AppendChild(doc.CreateElement("Name")).InnerText = "";
            ch.AppendChild(doc.CreateElement("TechnicalRequirement")).InnerText = "";
            ch.AppendChild(doc.CreateElement("Desctription")).InnerText ="";

            return new Cls_PTRow(ch);
        }

        //AddPTRow方法
        //添加时间：2014/8/6
        //添加人：李福安
        //修改时间：
        //修改人：
        public Cls_PTRow AddPTRow(int id, string name, string technicalrequirement, string desctription)
        {
            //find 
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;
            Cls_PTRow ExistCh = PTRow(id);

            if (null != ExistCh) return ExistCh;

            //add new 
            ch = _XmlNode.AppendChild(doc.CreateElement("PTRow"));
            ch.Attributes.Append(doc.CreateAttribute("id")).Value = id.ToString();

            //set property
            ch.AppendChild(doc.CreateElement("Name")).InnerText = name;
            ch.AppendChild(doc.CreateElement("TechnicalRequirement")).InnerText = technicalrequirement;
            ch.AppendChild(doc.CreateElement("Desctription")).InnerText = desctription;

            return new Cls_PTRow(ch);
        }

        //delete
        public void DeletePTRow(int id)
        {
            Cls_PTRow ch = this.PTRow(id);
            if (null == ch) return;

            XmlNode n = ch.Node.ParentNode;
            n.RemoveChild(ch.Node);
        }
    }

    public class Cls_PTRow : BaseNode
    {
        public Cls_PTRow(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }
        //技术要求
        public string TechnicalRequirement
        {
            get
            {
                return _XmlNode["TechnicalRequirement"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["TechnicalRequirement"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("TechnicalRequirement"));
                _XmlNode["TechnicalRequirement"].InnerText = value.ToString();
            }
        }

        public string Desctription
        {
            get
            {
                return _XmlNode["Desctription"].InnerText;
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["Desctription"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("Desctription"));
                _XmlNode["Desctription"].InnerText = value.ToString();
            }
        }
    }
}
